var MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

// Connection URL
const url = 'mongodb://localhost:27017';
 
// Database Name
const dbName = 'admin';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
    assert.equal(null, err);
    console.log("Connected successfully to server");

    const db = client.db(dbName);
    console.info('db:::::::::', db);
    // insertDocuments(db, function(result) {
    //     console.log('result:::::', result);
    //     client.close();
    // });

    insertAdmin(db, function(res) {
      console.log('res----', res);
      client.close();
    })
    // client.close();
});

const insertDocuments = function(db, callback) {
    // Get the documents collection
    const collection = db.collection('users');
    // Insert some documents
    collection.insertMany([
      {a : 1}, {a : 2}, {a : 3}, {a : 4}
    ], function(err, result) {
    //   console.log('result:::::', result);  
      assert.equal(err, null);
      assert.equal(4, result.result.n);
      assert.equal(4, result.ops.length);
      console.log("Inserted 3 documents into the collection");
      callback(result);
    });
  }

  const insertAdmin = function(db, callback) {
    const collection = db.collection('users');
    collection.insert({
      userName: 'superAdmin',
      role: 'superAdmin',
      password: '123456'
    }, function(err, res) {
      assert.equal(err, null);
      callback(res);
    })
  }